<template>
  <div class="app-container">
    <el-card>
      <div slot="header">
        系统设置
        <el-button style="float: right; padding: 3px 0" class="el-icon-back" type="text" @click="$router.go(-1)">返回</el-button>
      </div>
      <div style="display: flex;justify-content: center">
        <el-form
          v-if="initialized"
          ref="submitForm"
          v-loading="formLoading"
          style="width: 600px;"
          :model="submitForm"
          :rules="submitFormRules"
          label-width="120px"
          label-position="right"
          class="submit-form"
        >
          <el-form-item label="企业全称" prop="enterpriseFullName">
            <el-input v-model="submitForm.enterpriseFullName" placeholder="请输入企业全称" />
          </el-form-item>
          <el-form-item label="企业简称" prop="enterpriseName">
            <el-input v-model="submitForm.enterpriseName" placeholder="请输入企业简称" />
          </el-form-item>
          <el-form-item label="手机" prop="phoneNum">
            <el-input v-model="submitForm.phoneNum" placeholder="请输入手机号码" />
          </el-form-item>
          <el-form-item label="电话" prop="landlineNum">
            <el-input v-model="submitForm.landlineNum" placeholder="请输入电话号码" />
          </el-form-item>
          <el-form-item label="传真" prop="faxNum">
            <el-input v-model="submitForm.faxNum" placeholder="请输入传真号码" />
          </el-form-item>
          <el-form-item label="地址" prop="address">
            <el-input v-model="submitForm.address" type="textarea" maxlength="255" show-word-limit :autosize="{ minRows: 2}" placeholder="请输入企业地址" />
          </el-form-item>
          <el-form-item label="初始登录密码" prop="defaultPassword">
            <el-input v-model="submitForm.defaultPassword" type="password" show-password placeholder="请输入初始登录密码" />
          </el-form-item>
          <el-form-item label="二维码路径" prop="qrCodeUrl">
            <el-input v-model="submitForm.qrCodeUrl" placeholder="请输入二维码路径" />
          </el-form-item>
          <el-form-item label="税率%" prop="taxRate">
            <el-input v-model="submitForm.taxRate" placeholder="请输入税率%" type="number" />
          </el-form-item>
          <el-form-item>
            <el-button :loading="formLoading" type="primary" @click="handleSubmit">保存</el-button>
          </el-form-item>
        </el-form>
        <div v-else style="position: absolute;top: 50%;left: 50%;transform: translate(-50%, -50%)">
          <el-button :loading="formLoading" type="primary" size="large" @click="initClick">初始化</el-button>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script>
import { init, updateConfigById, selectConfig } from '@/api/system/config'
/**
 * @author yizuomin
 * @date 2023/5/7 19:05
 **/
export default {
  name: 'SubmitModule',
  data() {
    return {
      dialogVisible: this.visible,
      dialogTitle: '',
      formLoading: false,
      initialized: false,
      submitForm: {
        id: '',
        enterpriseFullName: '',
        enterpriseName: '',
        landlineNum: '',
        faxNum: '',
        phoneNum: '',
        defaultPassword: '',
        qrCodeUrl: '',
        shiftName: '',
        taxRate: ''
      },
      submitFormRules: {
        enterpriseFullName: [{ required: true, message: '请输入企业全称', trigger: 'change' }],
        enterpriseName: [{ required: true, message: '请输入企业名称', trigger: 'change' }],
        landlineNum: [{ required: true, message: '请输入电话号码', trigger: 'change' }],
        faxNum: [{ required: true, message: '请输入传真号码', trigger: 'change' }],
        phoneNum: [{ required: true, message: '请输入手机号码', trigger: 'change' }],
        address: [{ required: true, message: '请输入企业地址', trigger: 'change' }],
        defaultPassword: [{ required: true, message: '请输入初始登录密码', trigger: 'change' }],
        qrCodeUrl: [{ required: true, message: '请输入二维码路径', trigger: 'change' }],
        taxRate: [{ required: true, message: '请输入税率', trigger: 'change' }]
      }
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    init() {
      this.formLoading = true
      selectConfig().then(resp => {
        if (resp.success && resp.data) {
          this.submitForm = resp.data
          this.initialized = true
        }
      }).finally(() => {
        this.formLoading = false
      })
    },
    initClick() {
      this.formLoading = true
      init().then(resp => {
        if (resp.success) {
          this.$message.success('初始化成功')
          this.init()
        }
      }).finally(() => {
        this.formLoading = false
      })
    },
    handleSubmit() {
      this.$refs.submitForm.validate((valid) => {
        if (valid) {
          this.formLoading = true
          updateConfigById(this.submitForm).then(resp => {
            if (resp.success) {
              this.$message.success('保存成功')
              this.init()
            }
          }).finally(() => {
            this.formLoading = false
          })
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
